package com.autumn.platform.dao;

import com.autumn.platform.core.Utils;

public class DaoUtils {

    /**
     * 数据库ID是否匹配
     * 
     * @param databaseId 实际值
     * @param allowDatabaseIds 允许值
     * @return
     */
    public static boolean isMatchDatabase(String databaseId, String allowDatabaseIds) {
        if (!Utils.isBlank(databaseId) && !Utils.isBlank(allowDatabaseIds)) {
            String[] allows = allowDatabaseIds.split("\\s+");//使用空白字符分隔
            boolean mode = !"!".equals(allows[0]);//是否肯定模式
            if (mode) {//肯定模式
                for (int i = 0, l = allows.length; i < l; i++) {
                    if (databaseId.equalsIgnoreCase(allows[i])) {
                        return true;
                    }
                }
            } else {// 否定模式，从第1项开始匹配
                for (int i = 1, l = allows.length; i < l; i++) {
                    if (databaseId.equalsIgnoreCase(allows[i])) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    }
}
